package com.lg.happyfarmer.activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lg.happyfarmer.R;
import com.lg.happyfarmer.base.BaseActivity;
import com.lg.happyfarmer.bean.Bank;
import com.lg.happyfarmer.snackbar.BaseTransientBottomBar;
import com.lg.happyfarmer.snackbar.TopSnackBar;
import com.lg.happyfarmer.untils.KeywordsFlow;
import com.lg.happyfarmer.untils.SearchDataPojo;

public class MainActivity extends BaseActivity {

	private KeywordsFlow keywordsFlow;
	private String[]	keywords = new String[] { "教育", "其他", "行政", "管理", "媒体",
			"农业", "服务", "IT","房产","医疗","贸易" };

	private ArrayList<Bank> selected=new ArrayList<>();
	private Button btn_test;
	private ImageButton ib_back;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		refreshTags();
	}

	private void initView() {
		keywordsFlow = (KeywordsFlow) findViewById(R.id.keywordsflow);
		btn_test= (Button) findViewById(R.id.btn_test);
		ib_back= (ImageButton) findViewById(R.id.ib_back);
		ib_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		btn_test.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent it=new Intent(MainActivity.this,RightActivity.class);
				startActivity(it);
			}
		});
	}

	private void refreshTags() {

		keywordsFlow.setDuration(800l);
		keywordsFlow.setOnItemClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Bank temp= (Bank) ((TextView) v).getTag(R.id.tag_first);
				String keyword = temp.getName();
				if(selected.size()==2 && !temp.isFlag()){
					TopSnackBar.make(btn_test, "您最多只可以选择两个职业哦", BaseTransientBottomBar.LENGTH_LONG).show();
					return;
				}
				switch (keyword){
					case "教育":
						selectedSize(temp);
						if(temp.isFlag()){
							v.setBackgroundResource(R.mipmap.edu_narmal);
							temp.setFlag(false);
							selected.remove(temp);
						}else{
							v.setBackgroundResource(R.mipmap.edu_selected);
							temp.setFlag(true);
						}
						canleClick();
						break;
					case "行政":
						selectedSize(temp);
						if(temp.isFlag()){
							v.setBackgroundResource(R.mipmap.admin_normal);
							temp.setFlag(false);
							selected.remove(temp);
						}else{
							v.setBackgroundResource(R.mipmap.admin_selected);
							temp.setFlag(true);
						}
						canleClick();
						break;
					case "媒体":
						selectedSize(temp);
						if(temp.isFlag()){
							v.setBackgroundResource(R.mipmap.medium_normal);
							temp.setFlag(false);
							selected.remove(temp);
						}else{
							v.setBackgroundResource(R.mipmap.medium_selected);
							temp.setFlag(true);
						}
						canleClick();
						break;
					case "管理":
						selectedSize(temp);
						if(temp.isFlag()){
							v.setBackgroundResource(R.mipmap.manage_normal);
							temp.setFlag(false);
							selected.remove(temp);
						}else{
							v.setBackgroundResource(R.mipmap.mananger_selected);
							temp.setFlag(true);
						}
						canleClick();
						break;
					case "农业":
						selectedSize(temp);
						if(temp.isFlag()){
							v.setBackgroundResource(R.mipmap.agr_normal);
							temp.setFlag(false);
							selected.remove(temp);
						}else{
							v.setBackgroundResource(R.mipmap.agr_selected);
							temp.setFlag(true);
						}
						canleClick();
						break;
					case "服务":
						selectedSize(temp);
						if(temp.isFlag()){
							v.setBackgroundResource(R.mipmap.service_normal);
							temp.setFlag(false);
							selected.remove(temp);
						}else{
							v.setBackgroundResource(R.mipmap.service_selected);
							temp.setFlag(true);
						}
						canleClick();
						break;
					case "IT":
						selectedSize(temp);
						if(temp.isFlag()){
							v.setBackgroundResource(R.mipmap.it_normal);
							temp.setFlag(false);
							selected.remove(temp);
						}else{
							v.setBackgroundResource(R.mipmap.it_selected);
							temp.setFlag(true);
						}
						canleClick();
						break;
					case "房产":
						selectedSize(temp);
						if(temp.isFlag()){
							v.setBackgroundResource(R.mipmap.house_normal);
							temp.setFlag(false);
							selected.remove(temp);
						}else{
							v.setBackgroundResource(R.mipmap.house_selected);
							temp.setFlag(true);
						}
						canleClick();
						break;
					case "医疗":
						selectedSize(temp);
						if(temp.isFlag()){
							v.setBackgroundResource(R.mipmap.medical_normal);
							temp.setFlag(false);
							selected.remove(temp);
						}else{
							v.setBackgroundResource(R.mipmap.medical_selected);
							temp.setFlag(true);
						}
						canleClick();
						break;
					case "贸易":
						selectedSize(temp);
						if(temp.isFlag()){
							v.setBackgroundResource(R.mipmap.trade_narmal);
							temp.setFlag(false);
							selected.remove(temp);
						}else{
							v.setBackgroundResource(R.mipmap.trade_selected);
							temp.setFlag(true);
						}
						canleClick();
						break;
					case "其他":
						selectedSize(temp);
						if(temp.isFlag()){
							v.setBackgroundResource(R.mipmap.another_normal);
							temp.setFlag(false);
							selected.remove(temp);
						}else{
							v.setBackgroundResource(R.mipmap.another_selected);
							temp.setFlag(true);
						}
						canleClick();
						break;
				}

			}
		});
		feedKeywordsFlow(keywordsFlow, keywords);
		keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
	}

	private static void feedKeywordsFlow(KeywordsFlow keywordsFlow, String[] arr) {
//		Random random = new Random();
		for (int i = 0; i < arr.length;i++) {
//			int ran = random.nextInt(arr.length);
			String tmp = arr[i];
			keywordsFlow.feedKeyword(tmp);
		}
	}

	private void selectedSize(Bank bank){
		if(selected.size()==2){
			if(!bank.isFlag()){
				TopSnackBar.make(btn_test, "您最多只可以选择两个职业哦", BaseTransientBottomBar.LENGTH_LONG).show();
				return;
			}

		}else if(selected.size()>0 || selected.size()<2){
			btn_test.setEnabled(true);
			selected.add(bank);
		}else if(selected.size()==0 || selected.size()==1){
			selected.add(bank);
			btn_test.setEnabled(true);
		}
	}

	private void canleClick(){
		if(selected.size()==0){
			btn_test.setEnabled(false);
		}
	}


}
